package com.moxiao.yfjh.module.order.tradeRefund.brokeragewithdraw;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.module.order.controller.admin.brokeragewithdraw.vo.*;
import com.moxiao.yfjh.module.order.convert.brokeragewithdraw.BlindBrokerageWithdrawConvert;
import com.moxiao.yfjh.module.order.dal.dataobject.brokeragewithdraw.BlindBrokerageWithdrawDO;
import com.moxiao.yfjh.module.order.dal.mysql.brokeragewithdraw.BlindBrokerageWithdrawMapper;
import com.moxiao.yfjh.module.pay.service.transfer.PayTransferService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.order.enums.ErrorCodeConstants.BROKERAGE_WITHDRAW_NOT_EXISTS;

/**
 * 相亲佣金提现 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class BlindBrokerageWithdrawServiceImpl implements BlindBrokerageWithdrawService {

    @Resource
    private BlindBrokerageWithdrawMapper blindBrokerageWithdrawMapper;
    @Resource
    private PayTransferService payTransferService;

    @Override
    public Long createBrokerageWithdraw(BlindBrokerageWithdrawCreateReqVO createReqVO) {
        // 插入
        BlindBrokerageWithdrawDO brokerageWithdraw = BlindBrokerageWithdrawConvert.INSTANCE.convert(createReqVO);
        blindBrokerageWithdrawMapper.insert(brokerageWithdraw);
        // 返回
        return brokerageWithdraw.getId();
    }

    @Override
    public void updateBrokerageWithdraw(BlindBrokerageWithdrawUpdateReqVO updateReqVO) {
        // 校验存在
        validateBrokerageWithdrawExists(updateReqVO.getId());
        // 更新
        BlindBrokerageWithdrawDO updateObj = BlindBrokerageWithdrawConvert.INSTANCE.convert(updateReqVO);
        blindBrokerageWithdrawMapper.updateById(updateObj);
    }

    @Override
    public void deleteBrokerageWithdraw(Long id) {
        // 校验存在
        validateBrokerageWithdrawExists(id);
        // 删除
        blindBrokerageWithdrawMapper.deleteById(id);
    }

    private void validateBrokerageWithdrawExists(Long id) {
        if (blindBrokerageWithdrawMapper.selectById(id) == null) {
            throw exception(BROKERAGE_WITHDRAW_NOT_EXISTS);
        }
    }

    @Override
    public BlindBrokerageWithdrawDO getBrokerageWithdraw(Long id) {
        return blindBrokerageWithdrawMapper.selectById(id);
    }

    @Override
    public List<BlindBrokerageWithdrawDO> getBrokerageWithdrawList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return blindBrokerageWithdrawMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BlindBrokerageWithdrawDO> getBrokerageWithdrawPage(BlindBrokerageWithdrawPageReqVO pageReqVO) {
        return blindBrokerageWithdrawMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<BlindBrokerageWithdrawRespVO> selectJoinPage(BlindBrokerageWithdrawPageReqVO reqVO) {
        return blindBrokerageWithdrawMapper.selectJoinPage(reqVO);
    }

    @Override
    public List<BlindBrokerageWithdrawDO> getBrokerageWithdrawList(BlindBrokerageWithdrawExportReqVO exportReqVO) {
        return blindBrokerageWithdrawMapper.selectList(exportReqVO);
    }

    @Override
    public List<BlindBrokerageWithdrawDO> getList(BlindBrokerageWithdrawExportReqVO exportReqVO) {
        return blindBrokerageWithdrawMapper.selectList(exportReqVO);
    }
}
